$(document).ready(function() {
	//弹出jquery ui模态框就绪
	$("#shoppingModal").dialog({
		autoOpen: false,
		modal: true,
		width: 800,
		buttons: {
			"提交订单": function() {
				var numberSUM = "",
					ID_sum = "",
					Number_sum = "",
					Money_sum = "",
					reg = /^[0-9]*$/,
					flag = false;
				$(".shopNum").each(function(n1, v1) {
					if (reg.test($(this).val())) {
						if($(this).val()==""){
							flag = false;
							return false;
						}else{
							flag = true;
							numberSUM += $(this).val() + ",";
						}
					} else {
						flag = false;
						return false;
					};
				});
				if (flag) {
					$(".shopNum").each(function() {
						Number_sum += $(this).val() + ",";
					});
					$(".shopMoney").each(function() {
						Money_sum += $(this).text() + ",";
					});
					$.ajax({
						url: "/Handler/OrderHandler.ashx",
						type: "post",
						dataType: "json",
						data: {
							fun: "f_0003",
							//Order_GuestID: $("#orderFormShopping").parent().attr("dataid"),
							OrderProduct_ProductID: $("#typeID").val(),
							OrderProduct_Number: Number_sum.substr(0, Number_sum.length - 1),
							OrderProduct_Money: Money_sum.substr(0, Money_sum.length - 1)
						},
						success: function(msg) {
							if (msg.r_code == "1") {
								toastr.success(msg.r_info, '系统提示：');
								$("#orderFormShopping").parent().modal('hide');
								$("#orderFormShopping").parent().remove();
								//刷新页面
								setTimeout(function() {
									window.location.href = window.location.href;
								}, 1000);
							} else if (msg.r_code == "0") {
								toastr.error(msg.r_info, '错误提示：');
							}
						}
					});
					$(this).dialog("close");
				} else {
					toastr.error("请检查产品数量，必须是一个有效的数字", '<h4>错误提示：</h4>');
				};
			},
			"继续选购":function(){
			    $(this).dialog("close");
			}
		}
	});
	//点击购物车按钮事件
	$("#shoppingCart").click(function() {
		var flag = false;
		var checkedHTML = [];
		if ($("#typeID").val() !== "") { //如果value不为空
			var Money_sum = 0;
			flag = true;
			checkedHTML.push('<h4 class="text-success">辅助品</h4>');
			checkedHTML.push('<table class="table table-bordered table-condensed table-hover">');
			checkedHTML.push('<thead><tr><th>产品编码</th><th>产品名称</th><th>产品价格</th><th>数量</th><th>总金额</th></tr></thead><tbody>');
			//循环input传值框中 用,分隔开的数据
			var typeThis=$("#typeID")
			$.each(typeThis.val().split(","), function(n, v) {
				checkedHTML.push('<tr><td>' + typeThis.data("datalist").code.split(",")[n] + '</td>');
				checkedHTML.push('<td>' + typeThis.data("datalist").name.split(",")[n] + '</td>');
				checkedHTML.push('<td class="shopMoney">' + typeThis.data("datalist").money.split(",")[n] + '</td>');
				checkedHTML.push('<td><input class="shopNum input-mini" type="number" value="1"></td>');
				checkedHTML.push('<td class="allMoney"><span class="text-error">' + typeThis.data("datalist").money.split(",")[n] + '</span></td>');
				Money_sum += parseFloat(typeThis.data("datalist").money.split(",")[n]);
			});

			checkedHTML.push('</tbody><tfoot><tr>');
			checkedHTML.push('<th class="total-label" colspan="4" style="text-align: right;">小计</th>');
			checkedHTML.push('<th class="total-amount">￥： <strong class="text-error sumMoney">' + Money_sum + '</strong></th>');
			checkedHTML.push('</tr></tfoot></table>');
		}
		if (flag) {
			$("#shoppingModal").html(checkedHTML.join(""));
			//数量变化后 总金额变化事件
			var reg = /^[0-9]*$/;
			$(".shopNum").on("keyup",function(){
				if (reg.test($(this).val())) {
					var this_parent=$(this).parents("tr"),
						this_table=$(this).parents("table")

					this_parent.find(".allMoney").html('<span class="text-error">'+this_parent.find(".shopMoney").text()*$(this).val()+"</span>");
					var summoney=0;
					this_table.find(".allMoney").each(function(){
						summoney+=parseFloat($(this).text());
					});
					this_table.find(".sumMoney").text(summoney);
				};
			});
			//输入数量后价格变化
			$("#shoppingModal").dialog("open");
		} else {
			toastr.info("请先选择产品后，再加入购物车！！", '<h4>友情提示：</h4>');
		}
		return false;
	});
	$('#orderFormShopping').parent().on('hidden', function() {
		if ($("#shoppingModal").html() !== "") {
			$("#shoppingModal").dialog('destroy');
		};
	});
	//表格checkbox框点击事件
	$("#tableBODY .shopCheck").on("click", function() {
		var p_code = "",
			p_name = "",
			p_value = "",
			p_money = "";
		var typeid = $(this).attr("vid"); //获得隐藏input传值框id
		if ($("#tableBODY .shopCheck:checked").length === 0) {
			$("#typeID").val(""); //如果表格中没有选中checkbox，input传值框为空
		} else {
			//如果表格有选中checkbox，循环选中的项目
			$("#tableBODY .shopCheck:checked").each(function() {
				//组装数据
				p_code += $(this).parents("tr").find(".Product_Code").text() + ",";
				p_name += $(this).parents("tr").find(".Product_Name").text() + ",";
				p_value += $(this).val() + ",";
				p_money += $(this).parents("tr").find(".Product_Money").val() + ",";
				//为input传值框赋值
				$("#typeID").val(p_value.substr(0, p_value.length - 1));
				//为input传值框附加属性值
				$("#typeID").data("datalist", {
					code: p_code.substr(0, p_code.length - 1),
					name: p_name.substr(0, p_name.length - 1),
					money: p_money.substr(0, p_money.length - 1)
				});
			});
		};
	});
	//表格dataTable
	$('#tableBODY table').dataTable({
		"bJQueryUI": true,
		"sPaginationType": "full_numbers",
		"sDom": '<""l>t<"F"fp>',
		"oLanguage": {
	      "oPaginate": {
	      	"sPrevious": "上一页",
	        "sNext": "下一页",
	        "sLast": "末页",
	        "sFirst": "首页",
	        "sZeroRecords": "No records to display"
	      },
	      "sEmptyTable" :"没有可用数据表",
	      "sSearch": "搜索：",
	      "sLengthMenu": '每页显示 <select>'+
	        '<option value="10">10</option>'+
	        '<option value="20">20</option>'+
	        '<option value="30">30</option>'+
	        '<option value="50">50</option>'+
	        '</select> 条记录'
	    }
	});
	$('#tableBODY select').select2();
});